//
//  ViewController.m
//  LocalNotification
//
//  Created by 石虎 on 2017/11/4.
//  Copyright © 2017年 shihu. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    UIApplication *_app;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    _app = [UIApplication sharedApplication];
}
- (IBAction)changed:(id)sender
{
    UISwitch* sw = (UISwitch*) sender;
    if (sw.on)
    {
        
        if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
        }
        
        // 创建一个本地通知
        UILocalNotification *notification = [[UILocalNotification alloc]init];
        // 设置通知的触发时间
        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
        // 设置通知的时区
        notification.timeZone = [NSTimeZone defaultTimeZone];
        // 设置通知的重复发送的事件间隔
        notification.repeatInterval = kCFCalendarUnitHour;
        // 设置通知的声音
        notification.soundName = @"gu.mp3";
        //通知标题
        notification.alertTitle=@"我是标题";
        // 设置当设备处于锁屏状态时，显示通知的警告框下方的title
        notification.alertAction = @"打开";
        // 设置通知是否可显示Action
        notification.hasAction = YES;
        // 设置通过通知加载应用时显示的图片
        notification.alertLaunchImage = @"logo.png";
        // 设置通知内容
        notification.alertBody = @"亲,好几天????,想念你了!";
        // 设置显示在应用程序上红色徽标中的数字
        notification.applicationIconBadgeNumber = 10;
        // 设置userinfo，用于携带额外的附加信息。
        NSDictionary *info = @{@"bys": @"key"};
        notification.userInfo = info;
        // 调度通知
        [_app scheduleLocalNotification:notification];  // ①
    }
    else
    {
        // 获取所有处于调度中本地通知数组
        NSArray *localArray = [_app scheduledLocalNotifications];
        if (localArray)
        {
            for (UILocalNotification *noti in localArray)
            {
                NSDictionary *dict = noti.userInfo;
                if (dict)
                {
                    // 如果找到要取消的通知
                    NSString *inKey = [dict objectForKey:@"key"];
                    if ([inKey isEqualToString:@"bys"])
                    {
                        // 取消调度该通知
                        [_app cancelLocalNotification:noti];  // ②
                    }
                }
            }
        }
    }
}
@end
